package com.toocms.jinjuhang.ui.mine.account_management

import android.app.Application
import androidx.core.graphics.drawable.DrawableCompat
import androidx.databinding.ObservableArrayList
import androidx.databinding.ObservableField
import com.blankj.utilcode.util.ColorUtils
import com.blankj.utilcode.util.ResourceUtils
import com.toocms.jinjuhang.BR
import com.toocms.jinjuhang.R
import com.toocms.jinjuhang.config.Constants
import com.toocms.jinjuhang.config.Urls
import com.toocms.jinjuhang.config.UserRepository
import com.toocms.jinjuhang.data.User
import com.toocms.jinjuhang.data.center.BalanceRecordsData
import com.toocms.jinjuhang.ui.mine.account_management.recharge.RechargeFgt
import com.toocms.jinjuhang.widget.view_model.requestData
import com.toocms.tab.base.BaseModel
import com.toocms.tab.base.BaseViewModel
import com.toocms.tab.binding.ItemBinding
import com.toocms.tab.binding.command.BindingAction
import com.toocms.tab.binding.command.BindingCommand
import com.toocms.tab.bus.Messenger
import com.toocms.tab.bus.event.SingleLiveEvent

class AccountManagementModel(application: Application) : BaseViewModel<BaseModel>(application) {
    var p = 1
    var symbol: String = "0"
        set(value) {
            field = value
            //刷新明细
            refreshBindingCommand.execute()
        }
    val backIconObservableField by lazy {
        val rawDrawable = ResourceUtils.getDrawable(R.mipmap.icon_arrow_left_black).mutate()
        DrawableCompat.setTint(
            rawDrawable, ColorUtils.getColor(com.toocms.tab.R.color.qmui_config_color_white)
        )
        ObservableField(rawDrawable)
    }
    val dataObservableField = ObservableField<User>()
    val items = ObservableArrayList<AccountManagementRecordItemModel>()
    val itemBinding = ItemBinding.of<AccountManagementRecordItemModel>(
        BR.accountManagementRecordItemModel, R.layout.item_account_management_record
    )
    val backBindingCommand = BindingCommand<BindingAction>(this::finishFragment)
    val rechargeClickBindingCommand = BindingCommand<BindingAction>(BindingAction {
        startFragment(RechargeFgt::class.java)
    })

    val stopRefreshOrLoadSingleLiveEvent = SingleLiveEvent<Unit>()

    val refreshBindingCommand = BindingCommand<BindingAction>(BindingAction {
        refreshRecords()
    })

    val loadBindingCommand = BindingCommand<BindingAction>(BindingAction {
        loadRecords()
    })

    init {
        initializeMessenger()
        memberInfo(true)
    }

    private fun initializeMessenger() {
        Messenger.getDefault().register(this, Constants.MessengerToken.TOKEN_REFRESH_BALANCE) {
            memberInfo()
            refreshRecords()
        }
    }

    private fun refreshRecords(isShowLoading: Boolean = false) {
        p = 1
        balanceRecords(symbol, p, isShowLoading)
    }

    private fun loadRecords() {
        p++
        balanceRecords(symbol, p, false)
    }


    private fun memberInfo(isShowLoading: Boolean = false) {
        val params = mutableMapOf<String, String?>()
        params["member_id"] = UserRepository.user.member_id
        requestData(Urls.Center.MEMBER_INFO,
            params,
            resultClass = User::class.java,
            isShowLoading = isShowLoading,
            result = {
                dataObservableField.set(it)
            })
    }

    private fun balanceRecords(symbol: String, p: Int, isShowLoading: Boolean = false) {
        val params = mutableMapOf<String, String?>()
        params["member_id"] = UserRepository.user.member_id
        params["symbol"] = symbol
        params["p"] = p.toString()
        requestData(Urls.Center.BALANCE_RECORDS,
            params,
            resultClass = BalanceRecordsData::class.java,
            isShowLoading = isShowLoading,
            result = {
                if (1 == p) items.clear()
                it.list?.forEach {
                    items += AccountManagementRecordItemModel(this, ObservableField(it))
                }
            },
            finish = {
                stopRefreshOrLoadSingleLiveEvent.call()
            })
    }

}